//
//  WNPrivacyView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

class WNPrivacyView: UIView, UITextViewDelegate {
    
    static let shared = WNPrivacyView()
    
    lazy var bgView: UIView = {
        let view = UIView.init(frame: CGRect.zero)
        view.backgroundColor = .white
        view.wn_cornerWithBorder(10)
        return view
    }()
    
    lazy var privacyLabel: UILabel = {
        let label = UILabel()
        label.text = "尊敬的用戶"
        label.font = .wn_font(name: .bold, size: 18)
        label.textColor = .init(named: "black_101010")
        label.textAlignment = .center
        return label
    }()
    
    lazy var refuseBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle(" Cancel ", for: .normal)
        btn.setTitleColor(.init(named: "gray_CECECE"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 15)
        btn.titleLabel?.textAlignment = .center
        btn.backgroundColor = .init(named: "white_F3F3F3")
        btn.rx.tap.bind { [weak self] in
            UIView.animate(withDuration: 0.15) {
                if cydia && applications {
                    assert(false)
                }else {
                    exit(0)
                }
            }
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var agreeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("同意", for: .normal)
        btn.backgroundColor = .init(named: "red_FE95B4")
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 18)
        btn.rx.tap.bind { [weak self] in
            saveIsAgreePrivacy(true)
            hidenPrivacy()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var textView: IQTextView = {
        let view = IQTextView()
        view.font = .wn_font(name: .bold, size: 14)
        view.showsVerticalScrollIndicator = false
        view.wn_appendLinkString(string: "在使用 \(getAppName()) 之前，請仔細閱讀並理解")
        view.wn_appendLinkString(string: "《服務協議》", withURLString: WNConfigure.WNURL.wn_Agreement)
        view.wn_appendLinkString(string: "和")
        view.wn_appendLinkString(string: "《隱私政策》", withURLString: WNConfigure.WNURL.wn_Privacy)
        view.wn_appendLinkString(string: ".\n我們關心您的隱私和資料安全。我們透過展示廣告來保持此應用程式的免費。我們可以繼續使用您的資料來為您客製化廣告嗎？ \n您可以隨時在應用程式設定中更改您的選擇。我們的合作夥伴將收集資料並使用您裝置上的唯一識別碼向您顯示廣告。")
        view.textColor = .black
        view.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(named: "red_FE95B4")!]
        view.isEditable = false
        view.backgroundColor = .white
        view.delegate = self
        return view
    }()
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(white: 0, alpha: 0.55)
        addSubview(bgView)
        bgView.addSubview(privacyLabel)
        bgView.addSubview(refuseBtn)
        bgView.addSubview(agreeBtn)
        bgView.addSubview(textView)
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(280)
            make.height.equalTo(320)
        }
        privacyLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(14)
        }
        refuseBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(-65)
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize(width: 110, height: 36))
        }
        agreeBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(65)
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize(width: 110, height: 36))
        }
        textView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(44)
            make.bottom.equalTo(-68)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        wn_getTopVC()?.tabBarController?.wn_pushWebVC(URL.absoluteString, URL.absoluteString == WNConfigure.WNURL.wn_Privacy ? "隱私政策" : "服務協議")
        return false
    }
}

func showPrivacy() {
    WNPrivacyView.shared.frame = Screen.bounds
    wn_getTopVC()?.tabBarController?.view.addSubview(WNPrivacyView.shared)
}

func hidenPrivacy() {
    WNPrivacyView.shared.removeFromSuperview()
}
